package com.gzhryc.net_marketing.cloud.micro.api.controllers.callback;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.cloud.business.services.PayGoodsOrderService;
import com.gzhryc.net_marketing.cloud.business.services.TeamPayApplyService;
import com.gzhryc.net_marketing.cloud.micro.api.MicroConstants;
import com.gzhryc.pay.adaptation.weixin.DefaultWXPayConfig;
import com.gzhryc.pay.adaptation.weixin.WXPayUtils;
import com.gzhryc.pay.adaptation.weixin.msg.WXPayNotify;
import com.gzhryc.pay.adaptation.weixin.msg.WXPayNotifyResponse;
import com.gzhryc.pay.adaptation.weixin.msg.normal.WXPayNormalPayNotify;
import com.gzhryc.pay.adaptation.weixin.msg.normal.WXPayNormalRefundNotify;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WXRefundOrder;
import com.gzhryc.shared_device.common.payment.services.WXPayOrderService;
import com.gzhryc.shared_device.common.payment.services.WXRefundOrderService;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;

public class WxpayCallbackController {

	static Logger log = Logger.getLogger(WxpayCallbackController.class);

	@Route(value = "/paycall/market/wxpay/{appId}/pay")
	public WXPayNotifyResponse wxpay(HttpServletRequest request, HttpServletResponse response) {
		String appId = WebUtils.getString(request, "appId");
		try {
			DefaultWXPayConfig config = PayServiceFactory.self().getWXPayConfig(appId,EBusinessType.MARKET.index());
			WXPayNotify notify = WebUtils.getJsonRequest(request, WXPayNotify.class);
			log.info("{{0}}微信普通支付回调，回调内容：{{1:json}}", appId, notify);
			if (config != null && notify != null) {
				OperateInfo operateInfo = new OperateInfo();
				operateInfo.setProgramType(MicroConstants.PROGRAM_TYPE);
				operateInfo.setOperatorType(EOperatorType.System.index());
				operateInfo.setOperator("微信普通支付回调");
				operateInfo.setOperateNote("订单支付");

				WXPayNormalPayNotify payNotify = WXPayUtils.getWXPayNormalPayNotify(notify, config);
				WXPayOrder payOrder =  WXPayOrderService.self(BaseConstants.JDBC_KEY,new Date()).getByTradeNo(payNotify.getOut_trade_no());
				if (payOrder != null) {
					WXPayOrderService.self(BaseConstants.JDBC_KEY,new Date()).payResult(payOrder,payNotify);
					if (!PayGoodsOrderService.self().payResult(payOrder, operateInfo)) {
						log.error("微信普通支付回调业务处理失败，订单信息：{{0:json}}", payOrder);
					}
				}
			} else {
				log.error("未获取{{0}}的微信配置或空回调信息", appId);
			}
		} catch (Exception e) {
			log.error(appId + "微信支付回调错误，" + e.getMessage(), e);
		}
		return new WXPayNotifyResponse();
	}

	@Route(value = "/paycall/market/wxpay/{appId}/refund")
	public WXPayNotifyResponse wxRefund(HttpServletRequest request, HttpServletResponse response) {
		String appId = WebUtils.getString(request, "appId");
		try {
			DefaultWXPayConfig config = PayServiceFactory.self().getWXPayConfig(appId,EBusinessType.MARKET.index());
			WXPayNotify notify = WebUtils.getJsonRequest(request, WXPayNotify.class);
			log.info("{{0}}微信普通退款回调，回调内容：{{1:json}}", appId, notify);
			if (notify != null && config != null) {
				WXPayNormalRefundNotify refundNotify = WXPayUtils.getWXPayNormalRefundNotify(notify, config);

				OperateInfo operateInfo = new OperateInfo();
				operateInfo.setProgramType(MicroConstants.PROGRAM_TYPE);
				operateInfo.setOperatorType(EOperatorType.System.index());
				operateInfo.setOperator("微信普通支付回调");
				operateInfo.setOperateNote("订单退款");

				WXRefundOrder refundOrder = WXRefundOrderService.self(BaseConstants.JDBC_KEY,new Date()).refundResult(refundNotify);
				if (refundOrder != null) {
					WXPayOrder payOrder = WXPayOrderService.self(BaseConstants.JDBC_KEY,new Date())
							.getByTradeNo(refundOrder.getPayTradeNo());
					if (payOrder != null) {
						TeamPayApplyService.self().refundResult(payOrder, refundOrder);
						PayGoodsOrderService.self().refundResult(payOrder, refundOrder);
					}
				}
			} else {
				log.error("未获取{{0}}的微信配置或空回调信息", appId);
			}
		} catch (Exception e) {
			log.error(appId + "微信退款回调错误，" + e.getMessage(), e);
		}
		return new WXPayNotifyResponse();
	}
}
